from get_weth_attr import *

class City_Weather():
    def __init__(self,city_name,city_temp,city_humidity,city_pressure,
                 wind,sunrise,sunset,day_length,weather_description):

        self.city_name = city_name
        self.city_temp = city_temp
        self.city_humidity = city_humidity
        self.city_pressure = city_pressure
        self.wind = wind
        self.sunrise = sunrise
        self.sunset = sunset
        self.day_length = day_length
        self.weather_description = weather_description


def get_weather(city_name):
    """Return weather from openweathermap"""
    r = requests.get(f'https://api.openweathermap.org/data/2.5/weather?q={city_name}'
                 f'&appid={openweatherAPI}&units=metric')
    return json.loads(r.text)



def get_weather_attr(r):
    """Return object of class City_Weather by city-request"""

    city_name = r['name']
    temp = r['main']['temp']
    humidity = r['main']['humidity']
    pressure = r['main']['pressure']
    wind = r['wind']["speed"]
    sunrise = datetime.datetime.fromtimestamp(r['sys']['sunrise'])
    sunset = datetime.datetime.fromtimestamp(r['sys']['sunset'])
    day_length = sunset - sunrise
    weather_description = r["weather"][0]['main']

    # city_name = city_rename(city_name)
    # weather_description = weather_rename(weather_description)

    city_name = rename(city_name,city_dict)
    weather_description = rename(weather_description,weather_dic)

    return City_Weather(city_name=city_name,city_temp=temp,city_humidity=humidity,city_pressure=pressure,wind=wind,
                        sunrise=sunrise,sunset=sunset,day_length=day_length,weather_description=weather_description)

def print_weather(city_weather):
    """Print result"""

    print("\n","*"*30)
    return f'В городе {city_weather.city_name} \n'\
           f'Температура воздуха составит: {city_weather.city_temp} C° \n'\
           f'Влажность воздуха: {city_weather.city_humidity}%\n'\
           f'Давление: {city_weather.city_pressure} мм.р.т.\n'\
           f'Ветер: {city_weather.wind} м/с\n'\
           f'Восход: {city_weather.sunrise}\n'\
           f'Закат: {city_weather.sunset}\n'\
           f'Продолжительность дня: {city_weather.day_length}\n'\
           f'На улице: {city_weather.weather_description}'



if __name__ == "__main__":
    for city in city_dict.keys():
        r = get_weather(city)
        city_weather = get_weather_attr(r)
        print(print_weather(city_weather))


